#include "TimerOne.h"
/* first include Arduino.h, the IDE includes it after irmp*.h ... */
#include "Arduino.h"
/* ... and then chokes on uintX_t ... */


extern "C" {
#include "irmpconfig.h"
#include "irmp.h"
#include "irsndconfig.h"
#include "irsnd.h"
}


#define SER_BAUD 115200


/* F_INTERRUPTS is the interrupt frequency defined in irmpconfig.h */
#define US (1000000 / F_INTERRUPTS)


void setup()
{
  Serial.begin(SER_BAUD);
  /* greeting string and debugging ouput */
  Serial.println("Start Sniffing Session...");
  Serial.print("Interrupt each ");
  Serial.print(US);
  Serial.println(" µs");
  /* configure the input pin. IRMP_BIT is defined in irmpconfig.h,
     default is digital pin 2 */
  pinMode(IRMP_BIT, INPUT);
  delay(20); /* make sure the greeting string is out before starting */
  Timer1.initialize(US);
  // set the interrupt callback function
  Timer1.attachInterrupt(timerinterrupt);
  
}


IRMP_DATA irmp_data;
int count = 0;

void loop()
{
  if (irmp_get_data (&irmp_data))
  {
    Serial.print(irmp_data.protocol);
    Serial.print(":");
    Serial.print(irmp_data.address, HEX);
    Serial.print(":");
    Serial.print(irmp_data.command, HEX);
    Serial.print(":");
    Serial.print(irmp_data.flags, HEX);
    Serial.println("");
  }
}

/* helper function: attachInterrupt wants void(), but irmp_ISR is uint8_t() */
void timerinterrupt()
{
   uint8_t ir_detected;
   irmp_ISR(&ir_detected);
}



void toggle_led(int pin) {
    digitalWrite(pin, 1-digitalRead(3));
}
